package com.ctl.controller;

import com.ctl.config.LoginToken;
import com.ctl.config.SpringConfig;
import com.ctl.dto.CategoryDTO;
import com.ctl.entity.Category;
import com.ctl.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/categories")
public class CategoryController {
    @Autowired
    private CategoryService service;
    @DeleteMapping("/{id}")
    public void delCate(@PathVariable Integer id){
        service.removeById(id);
    }
    @GetMapping
    public List<CategoryDTO> getAll(){
        return service.getByCategorys();
    }

    @PatchMapping("/{id}")
    public Map<String,Object> upCategory(@PathVariable Integer id, @RequestBody Category category){
        category.setId(id);
        System.out.println("category = " + category);
        return service.updateByCategoryId(category);
    }
    @PostMapping
    public Category addCate(@RequestBody Category category){
        System.out.println("category = " + category);
        category.setCreatedAt(new Date());
        category.setUpdatedAt(new Date());
        service.save(category);
        return category;
    }
}
